#!/bin/bash  

##									配置						##
##文件夹绝对路径
file_floder=/home/z/shell/f

##文件名关键字 (xxx-20170101-1200.tar.gz里面的xxx)
file_names="test yyy xxx"

##拟删除几天之前的文件 
days_before=7

##不删除的文件名模式
save_file_format="-1200.tar.gz -0000.tar.gz"

##是否删除文件	(测试用)
delete_file="yes"

##是否打印日志 （测试用，yes就打印，其它值就不打印）
print_log="no"



##									代码						##
match=0
date_info=$(date -d "-$days_before days" +%Y%m%d)
for file_name in ${file_names[@]}
do
	if [[ $print_log == "yes" ]]; then
		echo "file_name = "$file_name
	fi
	max_deal_file_name=$file_floder/$file_name-$date_info
	file_list=$(find $file_floder -name "$file_name-20*-*")
	if [[ $print_log == "yes" ]]; then
		echo "max_deal_file_name = "$max_deal_file_name
		echo "file_list = "$file_list
	fi

	for one in ${file_list[@]}
	do
		if [[ $print_log == "yes" ]]; then
			echo "one = "$one
		fi
		if [[ $one < $max_deal_file_name ]]; then
			if [[ $print_log == "yes" ]]; then
				echo "real_deal = "$one
			fi
			for f in ${save_file_format[@]}
			do
				match=0
				if [[ $print_log == "yes" ]]; then
					echo "f = "$f
				fi
				if [[ $one == *$f ]]; then
					match=1
					if [[ $print_log == "yes" ]]; then
						echo "find one match format !"$one" "$f" "$match
					fi
					break
				else
					if [[ $print_log == "yes" ]]; then
						echo "not match !"$one" "$f" "$match
					fi
				fi
			done
			if [[ $print_log == "yes" ]]; then
				echo "match = "$match
			fi
			if [[ $match == 0 ]]; then
				if [[ $print_log == "yes" ]]; then
					echo "really not match ! prepare to remove "$one" "$match
				fi
				echo "delete file:"$one
				if [[ $delete_file == "yes" ]]; then
					rm -rf $one
				fi
			fi
		fi
	done
done

